﻿using System.Runtime.CompilerServices;
using UnityEngine;

namespace ProjectDanmaku.Util
{
    public static class CoordUtility
    {
        public static float CamBottom = -3;
        public static float CamTop = 3;
        public static float CamLeft = -5.3f;
        public static float CamRight = 5.3f;

        private static float _camWidth = CamRight - CamLeft;
        private static float _camHeight = CamTop - CamBottom;

        private static Vector2 _camSize = new Vector2(_camWidth, _camHeight);
        private static Vector2 _size = new Vector2(16, 9);

        public static readonly int PPU = 100;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Vector2 GetPosFromCoord(Vector2 coord)
        {
            var norm = (coord / _size) * _camSize;
            
            return new Vector2(CamLeft + norm.x, CamBottom + norm.y);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float PixelToUnit(int px)
        {
            return (float)px / PPU;
        }

        public static void CameraClip(Vector2 pos, ref Vector2 moveVec)
        {
            var tPos = pos + moveVec;

            if (tPos.x > CamRight)
                tPos.x = CamRight;
            else if (tPos.x < CamLeft)
                tPos.x = CamLeft;

            if (tPos.y > CamTop)
                tPos.y = CamTop;
            else if (tPos.y < CamBottom)
                tPos.y = CamBottom;

            moveVec = tPos - pos;
        }

        public static bool CheckOutOfScreen(Vector2 position)
        {
            return position.x <= CamLeft || position.x >= CamRight ||
                   position.y <= CamBottom || position.y >= CamTop;
        }
    }
}